<template>
	<view>
		<!-- 选择收货地址的盒子 -->
		<view class="address-box" v-if="JSON.stringify(address) === '{}'" >
		   <button type="primary" size="mini" @click="chooseAddress">请选择收获地址+</button>
		 </view>
		 
		 <!-- 收获信息的盒子 -->
		 <view class="adress-info-box" v-else @click="chooseAddress">
		   <view class="row1">
		    <view class="left-row1">
		      <view>收货人：{{address.userName}}</view>
		    </view>
		    <view class="right-row1">
		      <view class="phone">电话：{{address.telNumber}}</view>
		      <uni-icons type="arrowright" size="14"></uni-icons>
		    </view>
		   </view>
		   <view class="row2">
		    <view class="row2-left">收货地址：</view>
		    <view class="row2-right">{{addstr}}</view>
		   </view>
		 </view>
		 
		 <!-- 底部边框线 -->
		 <image src="../../static/cart_border@2x.png" mode="" class="address-border"></image>
	</view>
</template>

<script>
  // 1. 按需导入 mapState 和 mapMutations 这两个辅助函数
 import { mapState, mapMutations,mapGetters } from 'vuex'
	export default {
		data() {
			return {
				// 收获地址
				// address:{}
			};
		},
    computed:{
      ...mapState('m_user',['address']),
      ...mapGetters('m_user',['addstr'])
    },
    methods:{
      
      ...mapMutations('m_user',['updateAddress']),
      // // 选择收货地址
     async chooseAddress(){
       
        // 1. 调用小程序提供的 chooseAddress() 方法，即可使用选择收货地址的功能
         //    返回值是一个数组：第1项为错误对象；第2项为成功之后的收货地址对象
      const [err,res] = await uni.chooseAddress().catch(err =>err)
        console.log(res)
        if(err === null && res.errMsg === 'chooseAddress:ok') {
          // this.address = res
          // 更新 vuex 中的收货地址
          this.updateAddress(res)
        }
      // uni.chooseAddress({
      //   success(res){
      //     console.log(res)
      //     if(res.errMsg === 'chooseAddress:ok') {
      //       this.address = res
      //     }
      //   },
      // })
      }
    }
	}
</script>

<style lang="scss">
  
  .address-box{
    height: 90px;
  }
.adress-info-box{
   height: 90px;
     font-size: 12px;
     padding: 0 5px;
     display: flex;
     flex-direction: column;
     justify-content: center;
    .row1{
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      .right-row1{
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .phone{
          margin-right: 5px;
        }
      }
    }
    
    .row2 {
      display: flex;
      // justify-content: space-between;
      // align-items: center;
      margin-top: 10px;
      
      .row2-left{
       white-space: nowrap;
      }
    }
  }
.address-border{
    display: block;
    height: 5px;
    width: 100%;
}

.address-box {
  height: 90px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
